# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, _, tools


class FapiaoPaperLine(models.Model):
    _inherit = 'fp.fapiao.paper.line'
    invoice_line_ids = fields.One2many(
        'fp.input.inv.line', 'paper_line_id',
        help='从input.inv创建paper, 用于反向设置paper_line_id')

    discount_invoice_line_ids = fields.One2many(
        'fp.input.inv.line', 'discount_paper_line_id',
        help='从input.inv创建paper, 用于反向设置discount_paper_line_id')


class InputInvoiceLine(models.Model):
    _name = 'fp.input.inv.line'
    _description = 'Fapiao Invoice Line'
    _order = 'fapiao_id, sequence, id'

    fapiao_id = fields.Many2one(
        'fp.input.inv', string='Fapiao',
        required=True, ondelete='cascade', index=True, copy=False)

    company_id = fields.Many2one(related='fapiao_id.company_id')
    type = fields.Selection(related='fapiao_id.type')
    invoice_type = fields.Selection(related='fapiao_id.invoice_type')
    state = fields.Selection(related='fapiao_id.state')

    name = fields.Char(string='Label')
    sequence = fields.Integer(string='Sequence', default=10)

    product_id = fields.Many2one(
        'fp.product', string='Fapiao Product', required=True, index=True)
    vat_product_id = fields.Many2one(
        string='Vat Product', related='product_id.vat_product_id')
    vat_product_code = fields.Char(
        'Vat Product Code', related='product_id.vat_product_code')
    vat_product_name = fields.Char(
        'Vat Product Name', related='product_id.vat_product_name')
    product_code = fields.Char('Product code', related='product_id.code')
    product_name = fields.Char('Product Name', related='product_id.name')
    product_spec = fields.Char('Spec', related='product_id.spec')
    product_uom = fields.Char('Uom', related='product_id.uom')
    tax_id = fields.Many2one('fp.tax', 'Tax Ratio Set', required=True)
    tax_ratio = fields.Float('tax_ratio', related='tax_id.tax_ratio')

    @api.onchange('product_id')
    def on_change_product_id(self):
        for one in self:
            one.tax_id = one.product_id.tax_id
            one.price_unit = one.product_id.price_unit

    paper_line_id = fields.Many2one(
        'fp.fapiao.paper.line', 'Paper Line', readonly=True)
    discount_paper_line_id = fields.Many2one(
        'fp.fapiao.paper.line', 'Discount Paper Line', readonly=True)

    row_number = fields.Integer(
        'Row Number', compute='_compute_paper_line')
    discount_row_number = fields.Integer(
        'Discount Row Number', compute='_compute_paper_line')

    @api.depends('product_id')
    def _compute_paper_line(self):
        for line in self:
            pln = line.paper_line_id
            dis = line.discount_paper_line_id
            line.row_number = pln.row_number or 0
            line.discount_row_number = dis.row_number or 0

    quantity = fields.Float(
        string='Quantity', required=True, default=1.0)
    price_unit = fields.Float('Unit Price', default=1.0, help="含税单价")

    amount_untaxed = fields.Float('Untaxed Amount', compute='_compute_amount')
    amount_tax = fields.Float('Tax', compute='_compute_amount')
    amount_total = fields.Float('Total', compute='_compute_amount')
    price_untax = fields.Float('Unit Price Untax', compute='_compute_amount')

    amount_discount = fields.Float('Discount', default=0.0)

    amount_untaxed_discount = fields.Float(
        'Untaxed Discount Amount', compute='_compute_discount')
    amount_tax_discount = fields.Float(
        'Tax Discount', compute='_compute_discount')

    @api.depends('quantity', 'price_unit')
    def _compute_amount(self):
        for line in self:
            amount_total = line.quantity * line.price_unit
            amount_total = round(amount_total, 2)
            untaxed = amount_total / (1+line.tax_ratio/100)
            untaxed = round(untaxed, 2)
            tax = round(amount_total - untaxed, 2)

            line.amount_total = amount_total
            line.amount_untaxed = untaxed
            line.amount_tax = tax

            if line.quantity and line.price_unit:
                price_untax = untaxed / line.quantity
                price_untax = round(price_untax, 8)
            else:
                price_untax = 0.0

            line.price_untax = price_untax

    @api.depends('amount_discount')
    def _compute_discount(self):
        for line in self:
            untaxed = line.amount_discount / (1+line.tax_ratio/100)
            untaxed = round(untaxed, 2)
            tax = round(line.amount_discount - untaxed, 2)
            line.amount_untaxed_discount = untaxed
            line.amount_tax_discount = tax

    def _vals_list_for_paper(self):
        vals_list = []
        index = 0
        for line in self:
            index = index + 1
            vals = {
                'name': line.name,
                'sequence': line.sequence,
                'product_id': line.product_id.id,
                'tax_ratio': line.tax_ratio}

            vals_line = vals.copy()

            vals_line.update({
                'line_type': 'line',
                'row_number': index,
                'ref_row_number': 0,
                'invoice_line_ids': [(5, 0, 0), (4, line.id, 0)],
                'discount_invoice_line_ids': [(5, 0, 0)],
                'quantity': line.quantity,
                'price_unit': line.price_unit,
                'price_untax': line.price_untax,
                'amount_total': line.amount_total,
                'amount_untaxed': line.amount_untaxed,
                'amount_tax': line.amount_tax,
            })

            vals_list.append(vals_line)

            if line.amount_discount:
                index = index + 1
                vals_dis = vals.copy()
                vals_dis.update({
                    'line_type': 'discount',
                    'row_number': index,
                    'ref_row_number': index - 1,
                    'invoice_line_ids': [(5, 0, 0)],
                    'discount_invoice_line_ids': [(5, 0, 0), (4, line.id, 0)],
                    'quantity': 0,
                    'price_unit': 0.0,
                    'price_untax': 0.0,
                    'amount_total': line.amount_discount,
                    'amount_untaxed': line.amount_untaxed_discount,
                    'amount_tax': line.amount_tax_discount,

                })

                vals_list.append(vals_dis)

        return vals_list
